package com.vrv.netmgr.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.util.Log;

import com.vrv.common.GlobalConstants;
import com.vrv.netmgr.EdpApplication;
import com.vrv.netmgr.database.downservice.AlarmStatisticsDown;
import com.vrv.netmgr.database.downservice.EquipmentChangedDown;
/**
 * 离线数据更新service
 * @author zxb
 *
 */
public class OfflineDownService extends Service {
	private static final String TAG = OfflineDownService.class.getSimpleName();
	private static final boolean DEBUG = true && GlobalConstants.DEBUG;
	
	private Handler mHandler;
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		if (DEBUG) Log.e(TAG, "onCreate()");
	}

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
		if (DEBUG) Log.e(TAG, "onStartCommand()");
		
		// 网络状态判断
		boolean netStatus = EdpApplication.isNetStatus();
		
		if (netStatus) {
			Log.v(TAG, "net status is on line");
			
			HandlerThread handlerThread = new HandlerThread("cachethread");
			handlerThread.start();
			mHandler = new Handler(handlerThread.getLooper());
			mHandler.post(mRunnable);
		}else {
			Log.v(TAG, "net status is off line");
		}
		
        return super.onStartCommand(intent, flags, startId);
    }
	
	private Runnable mRunnable = new Runnable() {
		@Override
		public void run() {
			if (DEBUG) Log.e(TAG, "mRunnable.run()");
			
			AlarmStatisticsDown.startCache();
			EquipmentChangedDown.startCache();
		}
	};
}
